package MapReduce.writableBean;

import org.apache.hadoop.io.WritableComparable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

/**
 * @Author lixinlei
 * @Date 2023/3/21 11:07
 */
public class GenderWritable implements WritableComparable<GenderWritable> {

    private String groupName = "";
    private String personName = "";
    private int age;

    public String getGroupName() {
        return groupName;
    }

    public void setGroupName(String groupName) {
        this.groupName = groupName;
    }

    public String getPersonName() {
        return personName;
    }

    public void setPersonName(String personName) {
        this.personName = personName;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public int compareTo(GenderWritable o) {

        String thisGroupName = this.groupName;
        String thatGroupName = o.getGroupName();

        String thisPersonName = this.personName;
        String thatPersonName = o.getPersonName();

        int thisAge = this.age;
        int thatAge = o.getAge();

        if(thisGroupName.equals(thatGroupName)){
            if(thisPersonName.equals(thatPersonName)){
                return thisAge-thatAge;
            }else{
                return thisPersonName.compareTo(thatPersonName);
            }
        }else{
            return thisGroupName.compareTo(thatGroupName);
        }

    }

    @Override
    public void write(DataOutput out) throws IOException {

        out.writeUTF(groupName);
        out.writeUTF(personName);
        out.writeInt(age);
    }

    @Override
    public void readFields(DataInput in) throws IOException {
        this.groupName = in.readUTF();
        this.personName = in.readUTF();
        this.age = in.readInt();
    }

    @Override
    public String toString() {
        return this.groupName+"\t"+this.personName+"\t"+age;
    }
}
